package vn.allmobile.newsreader.adapters;

import java.util.List;

import vn.allmobile.newsreader.NewsDetailActivity;
import vn.allmobile.newsreader.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class NewsFlipAdapter extends BaseAdapter {

	List<NewsItem> mListNewsItem;
	Context mContext;
	Activity mActivity;

	public NewsFlipAdapter(Activity a, Context context, List<NewsItem> list) {
		this.mContext = context;
		this.mListNewsItem = list;
		this.mActivity = a;
	}

	@Override
	public int getCount() {
		return (int) Math.ceil((float) this.mListNewsItem.size() / 6);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View mView = convertView;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) parent.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mView = inflater.inflate(
					R.layout.news_activity, null);
		}
		ListView mGrid = (ListView) mView.findViewById(R.id.news_listView);

		NewsAdapter mAdapter = new NewsAdapter(mContext, this.mListNewsItem,
				position);
		mGrid.setAdapter(mAdapter);
		mGrid.setOnItemClickListener(clickItem);
		return mView;
	}

	private OnItemClickListener clickItem = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long id) {
			NewsItem mItem = (NewsItem) arg0.getItemAtPosition(position);
			// Get ID and show
			Intent mIntent = new Intent(mContext, NewsDetailActivity.class);
			mActivity.startActivity(mIntent);
		}
	};
}
